var app=new Vue({
    el:'#app',
    data:{
        list:[
            {
                name:'电子产品',
                content:[
                    {
                        id:1,
                        name:'iPhone 11',
                        price:6288,
                        count:1,
                        check:false
                    },
                    {
                        id:2,
                        name:'Huawei Mate 30',
                        price:5888,
                        count:1,
                        check:false
                    },
                    {
                        id:3,
                        name:'XiaoMi 9',
                        price:2899,
                        count:1,
                        check:false
                    },
                    {
                        id:4,
                        name:'XiaoMi Mix α',
                        price:19999,
                        count:1,
                        check:false
                    }
                ]
            },
            {
                name:'图书',
                content:[
                    {
                        id:1,
                        name:'《Java从入门到入坟》',
                        price:60,
                        count:1,
                        check:false
                    },
                    {
                        id:2,
                        name:'《Python从入门到入坟》',
                        price:50,
                        count:1,
                        check:false
                    },
                    {
                        id:3,
                        name:"《MySQL从入门到入坟》",
                        price:40,
                        count:1,
                        check:false
                    },
                    {
                        id:4,
                        name:'《JavaScript从入门到入坟》',
                        price:30,
                        count:1,
                        check:false
                    }
                ]
            }
        ],
        checkAll:false,
        smallHand:false
    },
    computed:{
        totalPrice:function(){
            var arr=[];
            for(var i=0;i<this.list.length;i++){
                arr=arr.concat(this.list[i]['content']);
            }
           this.newList=arr.filter(function(item){
                    if(item.check){
                        return item;
                    }
            });
            var total=0;
            for(var i=0;i<this.newList.length;i++){
                var item=this.newList[i];
                total+=item.price*item.count;
            }
            return total.toString();
        },
        allListNum:function(){
            var allNum=0;
            for(var i=0;i<this.list.length;i++){
                var item=this.list[i]['content'];
                for(var j=0;j<item.length;j++){
                    allNum++;
                }              
            }
            return allNum;
        }
    },
    methods:{
        handleReduce:function(index,smallIndex){
            console.log(index);
            console.log(smallIndex);
            if(this.list[index]['content'][smallIndex].count===1) return;
            this.list[index]['content'][smallIndex].count--;
        },
        handleAdd:function(index,smallIndex){
            this.list[index]['content'][smallIndex].count++;
        },
        handleRemove:function(index,smallIndex){
            this.list[index]['content'].splice(smallIndex,1);
            var num=0;
            for(var i=0;i<this.list.length;i++){
                var item=this.list[i]['content'];
                for(var j=0;j<item.length;j++){
                    if(item[j].check){
                        num++;
                    }else{
                        num--;
                    }
                }              
            }
            if(num==this.allListNum){
                this.checkAll=true;
            }else{
                this.checkAll=false;
            }
        },
        isAll:function(index,smallIndex){
            console.log(this.list[index]['content'][smallIndex].check);
            var indexItem=this.list[index]['content'][smallIndex];
            
            indexItem.check=!indexItem.check;
            var num=0;
            for(var i=0;i<this.list.length;i++){
                var item=this.list[i]['content'];
                for(var j=0;j<item.length;j++){
                    if(item[j].check){
                        num++;
                    }else{
                        num--;
                    }
                }              
            }
            console.log(num);
           // if(num==7||(num==5&&indexItem.check==false)){    这里的值不能写死，太笨了

           if(num==this.allListNum||(num==(this.allListNum-2)&&indexItem.check==false)){ 
                this.checkAll=indexItem.check;
                this.smallHand=true;
            }
        }
    },
    watch:{
        checkAll:function(){
            if(!this.smallHand){
                for(var i=0;i<this.list.length;i++){
                    var list=this.list;
                    for(var j=0;j<list[i]['content'].length;j++){
                        this.list[i]['content'][j]['check']=this.checkAll;
                    }
                }
            }
            this.smallHand=false;           
        }
    }
})